package com.huabo.monitor.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.hbfk.util.JsonBean;
import com.hbfk.util.PageInfo;
import com.hbfk.util.ResponseFormat;
import com.huabo.monitor.oracle.entity.TblAssEleCategory;
import com.huabo.monitor.oracle.entity.TblAssesselement;
import com.huabo.monitor.oracle.mapper.TblAssesselementMapper;
import com.huabo.monitor.service.TblAssEleCategoryService;
import com.huabo.monitor.service.TblAssessElementService;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

@Service("TblAssessElementService")
public class TblAssessElementServiceImpl implements TblAssessElementService {

    @Resource
    private TblAssesselementMapper tblAssesselementMapper;

    @Resource
    private TblAssEleCategoryService tblAssEleCategoryService;

    @Resource
    private TblAssessElementService tblAssessElementService;


    /**
     * 分页查询 TblAssesselement 列表数据
     *
     * @param pageNumber
     * @param pageSize
     * @param assesselement
     * @return
     */
    @Override
    public JsonBean findByPageInfo(Integer pageNumber, int pageSize, TblAssesselement assesselement) {
        if (pageNumber == null) {
            pageNumber = 1;
        }
        if (pageSize <= 0 || pageSize > 15) {
            pageSize = 15;
        }
        PageInfo<TblAssesselement> pageInfo = new PageInfo<>();
        pageInfo.setCondition(assesselement);
        pageInfo.setPageSize(pageSize);
        pageInfo.setCurrentPage(pageNumber);

        List<TblAssesselement> list = tblAssesselementMapper.findByPageInfo(pageInfo);
        Integer count = tblAssesselementMapper.findByCount(pageInfo);
        pageInfo.setTlist(list);
        pageInfo.setTotalRecord(count);
        return ResponseFormat.retParam(1, 200, pageInfo);
    }

    /**
     * 新增 TblAssesselement
     *
     * @param tblAssesselement
     */
    @Override
    public JsonBean add(TblAssesselement tblAssesselement) {
        tblAssesselementMapper.insert(tblAssesselement);
        return ResponseFormat.retParam(1, 200, "添加成功");
    }

    /**
     * 更具id更新 TblAssesselement
     *
     * @param tblAssesselement
     * @return
     */
    @Override
    public JsonBean update(TblAssesselement tblAssesselement) {
        if (null == tblAssesselement) {
            return ResponseFormat.retParam(0, 10002, "参数为空,请检查重试");
        }
        BigDecimal asseleid = tblAssesselement.getAsseleid();
        TblAssesselement dbAssesselement = tblAssesselementMapper.selectById(asseleid);
        if (null == dbAssesselement) {
            return ResponseFormat.retParam(0, 10002, "更新数据不存在,请检查重试");
        }
        tblAssesselementMapper.updateById(tblAssesselement);
        return ResponseFormat.retParam(1, 200, "更新成功");
    }

    /**
     * 通过 id 删除 TblAssesselement 对象
     *
     * @param
     * @return
     */
    @Override
    public JsonBean deleteByIds(String deleteIds) {
        if (StringUtils.isNotBlank(deleteIds)) {
            String[] ids = deleteIds.split(",");
            StringBuffer sb = new StringBuffer();
            for (String id : ids) {
                List<TblAssEleCategory> assEleCategories = tblAssEleCategoryService
                        .getAssesscategoryBytmplId(new BigDecimal(id));
                if (assEleCategories.size() > 0) {
                    sb.append("," + assEleCategories.get(0).getAssesselement().getElementNumber());
                } else {
                    tblAssessElementService.delete(tblAssessElementService.get(new BigDecimal(id)).getAsseleid());
                }
            }
            if (sb.length() > 0) {
                //return JsonBean.success("编号：" + sb.toString().substring(1, sb.toString().length()) + ",正在被模板使用,不能删除");
                return ResponseFormat.retParam(1, 200, "编号：" + sb.toString().substring(1, sb.toString().length()) + ",正在被模板使用,不能删除");
            }
            //return JsonBean.success("删除成功");
            return ResponseFormat.retParam(1, 200, "删除成功");
        }
        //return JsonBean.error();
        return ResponseFormat.retParam(0, 1000, "操作失败");
//        tblAssesselementMapper.deleteId(deleteId);
//        return ResponseFormat.retParam(1, 200, "删除成功");
    }

    @Override
    public List<TblAssesselement> getAssEssByIn(String assessIds) {
        return tblAssesselementMapper.getAssEssByIn(assessIds);
    }

    @Override
    public TblAssesselement get(BigDecimal id) {
        return  tblAssesselementMapper.get(id);

    }

    @Override
    public List<TblAssesselement> getComany(String orgid) {
        return tblAssesselementMapper.getComany(orgid);
    }

    @Override
    public TblAssesselement getNumber(String elementNumber, String orgid) {

        List<TblAssesselement> list =  this.tblAssesselementMapper.getNumber(elementNumber,orgid);
        if(list.size()>0){
            return list.get(0);
        }
        return null;
    }

    @Override
    public void delete(BigDecimal asseleid) {
        tblAssesselementMapper.deleteId(asseleid.toString());
    }

}
